using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace ERPStore.Tasks.Monitor.ViewModels
{
	public class BindingList<TModel> : System.ComponentModel.BindingList<TModel>, IRaiseItemChangedEvents, ITypedList
	{
		private bool _isSorted = false;
		[NonSerialized]
		private PropertyDescriptor _sortProperty;
		private ListSortDirection _sortDirection = ListSortDirection.Descending;
		[NonSerialized]
		private PropertyDescriptorCollection _shape;
		private bool _sortColumns = true;
		protected override bool SupportsSortingCore
		{
			get
			{
				return true;
			}
		}
		protected override ListSortDirection SortDirectionCore
		{
			get
			{
				return this._sortDirection;
			}
		}
		protected override PropertyDescriptor SortPropertyCore
		{
			get
			{
				return this._sortProperty;
			}
		}
		bool IRaiseItemChangedEvents.RaisesItemChangedEvents
		{
			get
			{
				return true;
			}
		}
		public BindingList(IList<TModel> list) : base(list)
		{
			this._sortColumns = true;
			this._shape = this.GetShape();
		}
		protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
		{
			List<TModel> items = base.Items as List<TModel>;
			if (items != null && null != prop)
			{
				PropertyComparer<TModel> pc = new PropertyComparer<TModel>(prop, direction);
				items.Sort(pc);
				this._isSorted = true;
			}
			else
			{
				this._isSorted = false;
			}
		}
		public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
		{
			PropertyDescriptorCollection pdc;
			if (null == listAccessors)
			{
				pdc = this._shape;
			}
			else
			{
				pdc = ListBindingHelper.GetListItemProperties(listAccessors[0].PropertyType);
			}
			return pdc;
		}
		public string GetListName(PropertyDescriptor[] listAccessors)
		{
			return typeof(TModel).Name;
		}
		private PropertyDescriptorCollection GetShape()
		{
			PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(typeof(TModel), new Attribute[]
			{
				new BrowsableAttribute(true)
			});
			if (this._sortColumns)
			{
				pdc = pdc.Sort();
			}
			return pdc;
		}
	}
}
